package com.hp.bon.sgw.threads;

import com.hp.bon.sgw.Constants;
import com.hp.bon.sgw.domain.TBusinessRecordApi;
import com.hp.bon.sgw.service.ConfigService;
import com.hp.bon.sgw.start.SpringContext;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MessageDetailCacheManager extends LogCacheManager {
    private static MessageDetailCacheManager _instance;

    public static MessageDetailCacheManager getInstance() {
        if (_instance == null) {
            _instance = new MessageDetailCacheManager();
        }

        return _instance;
    }

    public MessageDetailCacheManager() {
        setName("Task-TBusinessRecordApiCacheManager");
    }

    public void flush2File(Object e) {
        Constants.LOG.info("Too many TBusinessRecordApi Object in log buffer,lost " + e);
    }

    protected void flush2BD(List<Object> tempList) {
        ConfigService configService = SpringContext.getInstance().getConfigService();
        int ifInsert = configService.getIntValue("insert_t_message_detail", 0);
        if (ifInsert == 1) {
            SpringContext.getInstance().getOuterDBDataService().bulkSaveToDB(tempList);
        }

        int tApi = configService.getIntValue("insert_t_business_record_api", 0);
//        int tApi = configService.getIntValue("insert_t_business_record_api", 1);

        if (tApi == 1) {
            //T_BUSINESS_RECORD_API 日志添加
            List newTempList = new ArrayList();
            for (Iterator i$ = tempList.iterator(); i$.hasNext(); ) {
                Object o = i$.next();
                if ((o instanceof TBusinessRecordApi)) {
                    newTempList.add(o);
                }
            }

            if (!newTempList.isEmpty()) {
                SpringContext.getInstance().getOuterDBDataService().bulkSaveToDB(newTempList);
                StringBuilder sb = new StringBuilder();
                sb.append(getName()).append(" insert into DB t_business_record_api records num =");
                sb.append(newTempList.size()).append(",time(ms)=").append(System.currentTimeMillis() - this.runTime);
                Constants.LOG.info(sb.toString());
            }
        }
    }
}